#ifndef TILEMAP_H
#define TILEMAP_H

#include "Tileset.h"

#include <vector>

namespace GenBen {

struct TileInfo
{
	TileInfo() : tileset(0), tile_id(0) {}

	uint8 tileset;
	uint8 tile_id;
};

template<uint layers, uint width, uint height>
class Tilemap
{
public:
	Tilemap(Graphics* graphics, Tileset** tilesets, uint tileset_count)
		: graphics(graphics), tilesets(tilesets), tileset_count(tileset_count)
	{
	}
	virtual ~Tilemap() {};

	void drawLayer(uint tilemap_layer, uint physical_layer, const VectorI& pos)
	{
		TileInfo ti;

		VectorI tile_size = tilesets[0]->getTileSize();

		for(uint y = 0; y < height; ++y)
		{
			for(uint x = 0; x < width; ++x)
			{
				VectorI cursor = pos;
				
				cursor.x += x * tile_size.x;
				cursor.y += y * tile_size.y;

				if(cursor.x < (-1 * tile_size.x))
					continue;
				if(cursor.y < (-1 * tile_size.y))
					continue;
				
				if(cursor.x >= (signed)width*tile_size.x)
					continue;
				if(cursor.y >= (signed)height*tile_size.y)
					continue;

				ti = tilemap[tilemap_layer][x][y];

				// TEMP:
				if(ti.tile_id == 240) // Tile id 240 is always fully transparent
					continue;

				graphics->draw(tilesets[ti.tileset]->getTexture(), physical_layer, tilesets[ti.tileset]->getSourceRect(ti.tile_id), cursor);
				cursor.x += tile_size.x;
			}
		}
	}
	
	TileInfo tilemap[layers][width][height];

private:
	Graphics* graphics;
	Tileset** tilesets;
	uint tileset_count;
};

} // namespace

#endif